package com.xuecheng.managecmsclient.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author jl
 * @description
 * @date 2019-08-19 16:10
 */
@Configuration
public class RabbitMQConfig {
    /**
     * 队列bean的名称
     */
    private static final String QUEUE_CMS_POSTPAGE = "queue_cms_postpage";
    /**
     * 交换机的名称
     */
    private static final String EX_ROUTING_CMS_POSTPAGE = "ex_routing_cms_postpage";
    /**
     * 队列的名称
     */
    @Value("${xc.mq.queue}")
    public String queueName;
    /**
     * routingKey 即站点Id
     */
    @Value("${xc.mq.routingKey}")
    public String routingKey;

    /**
     * 声明direct类型交换机
     *
     * @return the exchange
     */
    @Bean(EX_ROUTING_CMS_POSTPAGE)
    public Exchange exchange() {
        return ExchangeBuilder.directExchange(EX_ROUTING_CMS_POSTPAGE).durable(true).build();
    }

    /**
     * 声明队列
     */
    @Bean(QUEUE_CMS_POSTPAGE)
    public Queue queue() {
        return new Queue(queueName);
    }

    /**
     * 绑定队列到交换机
     *
     * @param queue    the queue
     * @param exchange the exchange
     * @return the binding
     */
    @Bean
    public Binding binding(@Qualifier(QUEUE_CMS_POSTPAGE) Queue queue, @Qualifier(EX_ROUTING_CMS_POSTPAGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routingKey).noargs();
    }
}
